﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.BasicBase.BLL;
using Yipusoft.BasicBase.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// 需要传递一个参数：Request.QueryString["ParentID"]，ParentID是类别ID（字符型）
/// </summary>
public partial class Common_EquipStandard : System.Web.UI.Page
{
    //设置每页行数
    private int pageSize = 20;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid(0);
        }
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            string itemID = UserGrid.DataKeys[i].Value.ToString();
            TextBox itemCount = (TextBox)UserGrid.Rows[i].FindControl("ItemCount");
            DropDownList computeBy = (DropDownList)UserGrid.Rows[i].FindControl("ComputeBy");
            EquipStandardInfo stan = new EquipStandardInfo();

            stan.ItemID = itemID;
            stan.DepartmentID = "";
            stan.ItemCount = itemCount.Text == "" ? 0 : int.Parse(itemCount.Text);
            stan.ComputeBy = computeBy.SelectedValue;
            stan.TimeLastModified = DateTime.Now;
            EquipStandard.Insert(stan);
        }
    }
    public string GetItemName(string itemName, int itemLevel)
    {
        return "".PadLeft(2 * itemLevel, '　') + itemName;
    }
    public string GetItemCount(string itemID)
    {
        if (EquipStandard.Exsits(itemID))
            return EquipStandard.GetEquipStandard(itemID).ItemCount.ToString();
        else
            return "";
    }
    #region 通用代码

    /// <summary>
    /// 绑定GridView
    /// </summary>
    private void BindGrid(int pageIndex)
    {
        ViewState["PageIndex"] = pageIndex;
        StringBuilder selectCommandText = new StringBuilder();
        selectCommandText.Append("select * from codeitems where Groupid is null and left(itemid,len(itemid)-4)='" + Request.QueryString["ParentID"].Substring(0, Request.QueryString["ParentID"].Length - 4) + "'");
        UserGrid.DataSource = SplitPage.GetPageData(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        UserGrid.DataBind();

        int totalCount = SplitPage.GetRowCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        ViewState["PageCount"] = SplitPage.GetPageCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ViewState["PageIndex"]) * pageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            if (EquipStandard.Exsits(UserGrid.DataKeys[e.Row.RowIndex].Value.ToString()))
            {
                EquipStandardInfo standard = EquipStandard.GetEquipStandard(UserGrid.DataKeys[e.Row.RowIndex].Value.ToString());
                DropDownList computeBy = (DropDownList)e.Row.FindControl("ComputeBy");
                computeBy.SelectedIndex = computeBy.Items.IndexOf(computeBy.Items.FindByValue(standard.ComputeBy));
            }
        }
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(int.Parse(JumpList.SelectedValue));
    }
    #endregion
}
